package com.example.springboot1.task;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.Instant;
import java.util.Date;
import java.util.concurrent.ScheduledFuture;

/**
 * @author ckj
 * @description TODO
 * @date 2022-11-02 8:35
 */

@RestController
public class SchedueDemo {
    @Autowired
    private ThreadPoolTaskScheduler threadPoolTaskScheduler;



    @GetMapping("/sche")
    public void sche() {
        String corn = "0/4 * * * * ?";
        ScheduledFuture<?> schedule = threadPoolTaskScheduler.schedule(
                () -> {
                    System.out.println("开始任务" + Instant.now());
                }, triggerContext -> {
                    Date date = new CronTrigger(corn).nextExecutionTime(triggerContext);
                    return date;
                });
    }
}
